package com.windows;

import com.entity.Good;
import com.entity.Message;
import com.util.GoodUtil;
import com.util.LogUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class UpdateGood extends JDialog {

    /**
     * 标签和文本域和按钮
     * @author pc
     *
     */
    private JLabel namejl;
    private JLabel chargejl;
    private JLabel amountjl;
    private JLabel factoryjl;
    private JLabel markjl;
    private JLabel othjl;
    private JLabel headjl;

    private JTextArea name;
    private JTextArea charge;
    private JTextArea amount;
    private JTextArea factory;
    private JTextArea mark;
    private JTextArea oth;
    private JTextArea head;


    private JButton store;
    private JButton quit;

    private String t;
    private String n ;
    /**
     *
     * 加入一个good对象
     * @author pc
     *
     */
    private Good good;

    public UpdateGood(String table,String name) {

        this.t = table;
        this.n = name;
        //窗口初始化设置
        setSize(500, 500);
        setLocationRelativeTo(null);
        setResizable(false);                                   //此窗口大小不能改变
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("超市管理系统-------修改");
        Container c = getContentPane();
        setModal(true);
        c.setLayout(new GridLayout(8,1));

        getGood();


        addUI(c);
        addListener();





        setVisible(true);

    }


    private void addUI(Container c) {

        JPanel[] jps = new JPanel[8];

        for(int i =0 ;i<jps.length;i++) {

            jps[i] = new JPanel();
        }

        headjl = new JLabel("商品修改");
        headjl.setFont(new Font("微软雅黑",Font.BOLD,30));
        jps[0].add(headjl);

        name = new JTextArea();
        name.setColumns(17);
        name.setText(good.getName());
        name.setEnabled(false);
        name.setFont(new Font("宋体",Font.PLAIN,20));
        namejl = new JLabel("商品名称");
        namejl.setFont(new Font("宋体",Font.BOLD,20));

        jps[1].add(namejl);
        jps[1].add(name);

        chargejl = new JLabel("商品价格");
        chargejl.setFont(new Font("宋体",Font.BOLD,20));
        charge = new JTextArea();
        charge.setColumns(17);
        charge.setFont(new Font("宋体",Font.PLAIN,20));
        charge.setText(String.valueOf(good.getCharge()));
        jps[2].add(chargejl);
        jps[2].add(charge);

        amountjl = new JLabel("商品库存");
        amountjl.setFont(new Font("宋体",Font.BOLD,20));
        amount = new JTextArea();
        amount.setColumns(17);
        amount.setFont(new Font("宋体",Font.PLAIN,20));
        amount.setText(String.valueOf(good.getAmount()));
        jps[3].add(amountjl);
        jps[3].add(amount);

        factoryjl = new JLabel("生产厂家");
        factoryjl.setFont(new Font("宋体",Font.BOLD,20));
        factory = new JTextArea();
        factory.setColumns(17);
        factory.setFont(new Font("宋体",Font.PLAIN,20));
        factory.setText(good.getFactory());
        jps[4].add(factoryjl);
        jps[4].add(factory);

        mark = new JTextArea();
        mark.setColumns(17);
        mark.setFont(new Font("宋体",Font.PLAIN,20));
        markjl = new JLabel("商品品牌");
        markjl.setFont(new Font("宋体",Font.BOLD,20));
        mark.setText(good.getMark());
        jps[5].add(markjl);
        jps[5].add(mark);

        othjl = new JLabel("商品备注");
        othjl.setFont(new Font("宋体",Font.BOLD,20));
        oth = new JTextArea();
        oth.setColumns(17);
        oth.setFont(new Font("宋体",Font.PLAIN,20));
        oth.setText(good.getOth());
        jps[6].add(othjl);
        jps[6].add(oth);

        store = new JButton("保存");
        quit = new JButton("返回");



        jps[7].add(store);
        jps[7].add(quit);



        for(JPanel jp : jps) {

            c.add(jp);
        }

    }


    private void addListener() {

        //储存按钮
        store.addActionListener(new  ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {


                Good goodcopy = new Good();
                goodcopy.setName(name.getText());
                goodcopy.setCharge(Integer.valueOf(charge.getText()));
                goodcopy.setAmount(Integer.valueOf(amount.getText()));
                goodcopy.setFactory(factory.getText());
                goodcopy.setMark(mark.getText());
                goodcopy.setOth(oth.getText());

                Message message = GoodUtil.updateGood(goodcopy);

                JOptionPane.showMessageDialog(null, message.getMessage(), "修改结果",JOptionPane.PLAIN_MESSAGE);
                if(message.getFlag()) {
                    LogUtil.addlog("修改"+name.getText()+"成功");
                }else {
                    LogUtil.addlog("修改"+name.getText()+"失败");
                }


                UpdateGood.this.dispose();

            }
        });

        //返回按钮
        quit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {

                UpdateGood.this.dispose();
            }
        });


    }

    private void getGood() {

        Message message = GoodUtil.findByName(n);

        good = message.getGoods().get(0);
    }
}
